﻿
using System;
using System.Collections;
using System.Collections.Generic;



namespace DailyFx.IndicatorExtension
{

	public class SMA : Index
	{
		/// <summary>
		///		Circle Length, How many previous <see cref="Bar"/> required ??
		/// </summary>
		protected readonly int _span;


		/// <summary>
		/// c'tor
		/// </summary>
		public SMA ( int span, int n ) : base(1, n)
		{
			this._span = span;	
			this.MAimpl = new HyperVector ( n, this.Label, Layout.OVERLAY, Stylus.LINE, 0 );
		}


		public override void Cook ( BarSeries series )
		{

			decimal sum = 0.0m;
			decimal ma = 0.0m;

			decimal p = 0.0m;

			for (int n = 0; n < this.Length; n++)   // forming
			{
				//
				//
				decimal x = series[n].LastPx;

				sum += x;
				sum -= p;

				if (n < this._span ) // still preparing 
				{
					ma = sum / (n + 1);
				}
				else              // forming 
				{
					ma = sum / this._span;
					p = series [n - this._span].LastPx;
				}

				this.MAimpl[ n] = ma ;
			}
		}


		public virtual void Cook2 ( IVector target )
		{
			decimal sum = 0.0m;
			decimal ma = 0.0m;

			decimal p = 0.0m;

			for (int n = 0; n < target.Length; n++)   // forming
			{
				decimal x = target[n] ;

				sum += x;
				sum -= p;

				if ( n < this._span)  // still preparing 
				{
					ma = sum / (n + 1);
				}
				else              // forming 
				{
					ma = sum / this._span;
					p = target [ n - this._span];
				}

				this.MAimpl[ n] = ma ;
			}
		}


		public override string Family
		{
			get { return "Simple Moving Average"; }
		}

		public override string Label
		{
			get { return String.Format("SMA{0}", this.Span); }
		}



		public int Span
		{
			get {
				return this._span;
			}
			set {
				// this.mPeriod = value;
				// todo: notify perform reckon 
			}
		}


		public HyperVector MAimpl
		{
			get {
				return _children[ 0];
			}
			protected set {
				// assert ( _children[0] == null )
				_children[ 0] = value ; 
			}
		}

	}

  
}
